import { createRouter, createWebHistory } from 'vue-router'
import {routes} from "./routes"
import { ElMessage } from 'element-plus';
import {isLogin} from '@/utils/loginStateUtil';

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

//不参与过滤的路径
const notFilterPath=[
  "/user/login",
  "/404"
]

//守卫
router.beforeEach((to,from,next)=>{
  //判断是否是可以直接进入的页面
  if(notFilterPath.includes(to.path)){
    next()
    return
  }

  
  //判断用户是否登录,如果登录则放过
  const res = isLogin()
  console.log(`output->res`,res)
  if(res==0){
    next()
    return
  }

  //如果没登录则进行其他操作
  ElMessage.error(res==1?"您还未登录":"登录已过期")
  next({
    path:"/user/login"
  })

})

export default router
